package com.hqd.ch03.v50.boot.autoconfigure.web.servlet;

import com.hqd.ch03.v50.boot.autoconfigure.condition.ConditionalOnMissingBean;
import com.hqd.ch03.v50.boot.web.servlet.FilterRegistrationBean;
import com.hqd.ch03.v50.core.annotation.AliasFor;

import javax.servlet.Filter;
import java.lang.annotation.*;

/**
 * {@link Conditional @Conditional} that only matches when no {@link Filter} beans of the
 * specified type are contained in the {@link BeanFactory}. This condition will detect
 * both directly registered {@link Filter} beans as well as those registered via a
 * {@link FilterRegistrationBean}.
 * <p>
 * When placed on a {@code @Bean} method, the bean class defaults to the return type of
 * the factory method or the type of the {@link Filter} if the bean is a
 * {@link FilterRegistrationBean}:
 *
 * <pre class="code">
 * &#064;Configuration
 * public class MyAutoConfiguration {
 *
 *     &#064;ConditionalOnMissingFilterBean
 *     &#064;Bean
 *     public MyFilter myFilter() {
 *         ...
 *     }
 *
 * }</pre>
 * <p>
 * In the sample above the condition will match if no bean of type {@code MyFilter} or
 * {@code FilterRegistrationBean<MyFilter>} is already contained in the
 * {@link BeanFactory}.
 *
 * @author Phillip Webb
 * @since 2.1.0
 */
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@ConditionalOnMissingBean(parameterizedContainer = FilterRegistrationBean.class)
public @interface ConditionalOnMissingFilterBean {

    /**
     * The filter bean type that must not be present.
     *
     * @return the bean type
     */
    @AliasFor(annotation = ConditionalOnMissingBean.class)
    Class<? extends Filter>[] value() default {};

}